package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.*;
import com.javasm.service.MisMannageService.ProdService;
import com.javasm.service.MisMannageService.SeriesService;
import com.javasm.service.MisMannageService.TypeService;
import com.javasm.service.MisMannageService.impl.ProdServiceImpl;
import com.javasm.service.MisMannageService.impl.SeriesServiceImpl;
import com.javasm.service.MisMannageService.impl.TypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/23 21:21
 * @version:0.1
 * @since:jdk11
 */
@WebServlet("/prod/*")
public class ProdServlet extends BaseServlet {

    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        String queryProdName = req.getParameter("queryProdName");
        String queryAuditStatus = req.getParameter("queryAuditStatus");
        String querySecondTypeIdStr = req.getParameter("querySecondTypeId");

        Integer page = 1;
        Integer pageSize = 5;
        Integer querySecondTypeId = 0;

        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        if (querySecondTypeIdStr != null && !"".equals(querySecondTypeIdStr)) {
            querySecondTypeId = Integer.parseInt(querySecondTypeIdStr);
        }

        ProdInfo queryProd = new ProdInfo();
        queryProd.setProdName(queryProdName);
        queryProd.setAuditStatus(queryAuditStatus);
        queryProd.setSecondTypeId(querySecondTypeId);

        HttpSession session = req.getSession();
        com.javasm.entity.ManageSystemEntity.User user = (com.javasm.entity.ManageSystemEntity.User) session.getAttribute("loginUser");
        Integer loginUserId = user.getUserId();

        ProdService prs = new ProdServiceImpl();
        List<ProdInfo> prodByPage = prs.getProdByPage(page, pageSize, queryProd, loginUserId);
        Integer prodTotalNum = prs.prodTotalNum(queryProd, loginUserId);
        //System.out.println(loginUserId+"***********************");

        ReturnEntity re = new ReturnEntity();
        if (prodByPage.size() > 0) {
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(prodByPage);
            re.setPageInfo(new PageInfo(page,pageSize,prodTotalNum));
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
            re.setPageInfo(new PageInfo(1,1,0));
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void queryType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TypeService ts = new TypeServiceImpl();
        List<TypeInfo> secondSeriesName = ts.getSecondSeriesName();
        ReturnEntity re = new ReturnEntity();
        re.setRetData(secondSeriesName);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void addProd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ProdInfo insertProd = new ProdInfo();
        HttpSession session = req.getSession();
        com.javasm.entity.ManageSystemEntity.User user = (com.javasm.entity.ManageSystemEntity.User) session.getAttribute("loginUser");
        Integer userId = user.getUserId();
        insertProd.setCreateUserId(userId);

        String seriesIdStr = req.getParameter("seriesId");
        String secondTypeIdStr = req.getParameter("secondTypeId");

        String prodName = req.getParameter("prodName");
        insertProd.setProdName(prodName);

        String remitCompany = req.getParameter("remitCompany");
        insertProd.setRemitCompany(remitCompany);

        String rateYear = req.getParameter("rateYear");
        insertProd.setRateYear(rateYear);
        String moneyType = req.getParameter("moneyType");
        insertProd.setMoneyType(moneyType);
        String openingTimeDate = req.getParameter("openingTime");
        String subscribeCycleStr = req.getParameter("subscribeCycle");
        String fundManageRate = req.getParameter("fundManageRate");
        insertProd.setFundManageRate(fundManageRate);

        String subscribeRate = req.getParameter("subscribeRate");
        insertProd.setSubscribeRate(subscribeRate);

        String moneySubmitStyle = req.getParameter("moneySubmitStyle");
        insertProd.setMoneySubmitStyle(moneySubmitStyle);
        String redeemCycleStr = req.getParameter("redeemCycle");
        String redeemMinMoneyStr = req.getParameter("redeemMinMoney");
        String redeemFeeStr = req.getParameter("redeemFee");
        String closedCycleStr = req.getParameter("closedCycle");
        String reviewerIdStr = req.getParameter("reviewerId");
        String minMoneyStr = req.getParameter("minMoney");
        BigDecimal minMoney = null;
        if (minMoneyStr!=null && !"".equals(minMoneyStr)){
            minMoney = BigDecimal.valueOf(Double.parseDouble(minMoneyStr));
        }

        Integer seriesId = 0;
        if (seriesIdStr!=null && !"".equals(seriesIdStr)){
            seriesId = Integer.parseInt(seriesIdStr);
        }
        insertProd.setSeriesId(seriesId);
        Integer secondTypeId = 0;
        if (secondTypeIdStr!=null && !"".equals(secondTypeIdStr)){
            secondTypeId = Integer.parseInt(secondTypeIdStr);
        }
        insertProd.setSecondTypeId(secondTypeId);

        if (openingTimeDate!=null && !"".equals(openingTimeDate)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date openingTime = sdf.parse(openingTimeDate);
                insertProd.setOpeningTime(openingTime);
            } catch (ParseException e) {
                //insertProd.setOpeningTime(null);
                e.printStackTrace();
            }
        }

        Integer subscribeCycle = 0;
        if (subscribeCycleStr!=null && !"".equals(subscribeCycleStr)){
            subscribeCycle = Integer.parseInt(subscribeCycleStr);
        }
        insertProd.setSubscribeCycle(subscribeCycle);

        Integer redeemCycle = 0;
        if (redeemCycleStr!=null && !"".equals(redeemCycleStr)){
            redeemCycle = Integer.parseInt(redeemCycleStr);
        }
        insertProd.setRedeemCycle(redeemCycle);

        BigDecimal redeemMinMoney = null;
        if (redeemMinMoneyStr!=null && !"".equals(redeemMinMoneyStr)) {
            redeemMinMoney = BigDecimal.valueOf(Double.parseDouble(redeemMinMoneyStr));
        }
        insertProd.setRedeemMinMoney(redeemMinMoney);

        BigDecimal redeemFee = null;
        if (redeemMinMoneyStr!=null && !"".equals(redeemFeeStr)){
            redeemFee = BigDecimal.valueOf(Double.parseDouble(redeemFeeStr));
        }
        insertProd.setRedeemFee(redeemFee);

        Integer closedCycle = 0;
        if (closedCycleStr!=null && !"".equals(closedCycleStr)){
            closedCycle = Integer.parseInt(closedCycleStr);
        }
        insertProd.setClosedCycle(closedCycle);
        ReturnEntity re = new ReturnEntity();
        Integer reviewerId = 0;
        if (reviewerIdStr!=null && !"".equals(reviewerIdStr)){
            reviewerId = Integer.parseInt(reviewerIdStr);
        }
        insertProd.setReviewerId(reviewerId);

        ProdService ps = new ProdServiceImpl();
        Integer addProdNum = ps.insertProd(insertProd);

        if (addProdNum > 0) {
            re.setRetCode(ReturnCode.ADD_OK.getCode());
            re.setRetMsg(ReturnCode.ADD_OK.getMsg());
            //System.out.println("新增成功");
        } else {
            re.setRetCode(ReturnCode.ADD_FAILED.getCode());
            re.setRetMsg(ReturnCode.ADD_FAILED.getMsg());
            //System.out.println("新增失败");
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void querySeries(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SeriesService ss = new SeriesServiceImpl();
        List<ProdSeriesInfo> seriesName = ss.getSeriesName();

        ReturnEntity re = new ReturnEntity();
        if (seriesName.size() > 0) {
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(seriesName);
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();

    }

    protected void queryCompanyName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String seriesIdStr = req.getParameter("forCompanySeriesId");

        Integer seriesId = 0;
        if (seriesIdStr!=null && !"".equals(seriesIdStr)) {
            seriesId = Integer.parseInt(seriesIdStr);
        }

        SeriesService ss = new SeriesServiceImpl();
        String companyName = ss.getCompanyById(seriesId);

        ReturnEntity re = new ReturnEntity();
        if (companyName!=null) {
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(companyName);
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void queryOtherUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        com.javasm.entity.ManageSystemEntity.User user = (com.javasm.entity.ManageSystemEntity.User) session.getAttribute("loginUser");
        Integer userId = user.getUserId();
        //System.out.println("审核人编号："+userId);

        ProdService ps = new ProdServiceImpl();
        List<User> userToAudit = ps.getUserToAudit(userId);

        ReturnEntity re = new ReturnEntity();
        if (userToAudit!=null) {
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(userToAudit);
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void addAsset(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ProdInfo assetProd = new ProdInfo();
        String prodIdStr = req.getParameter("prodId");
        String assetValueStr = req.getParameter("assetValue");
        String assetBaseDate = req.getParameter("assetBaseDate");
        String countGrowthRate = req.getParameter("countGrowthRate");

        Integer prodId = 0;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.parseInt(prodIdStr);
        }

        BigDecimal assetValue = null;
        if (assetValueStr != null && !"".equals(assetValueStr)) {
            assetValue = BigDecimal.valueOf(Double.parseDouble(assetValueStr));
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date assetBaseTime = null;
        try {
            assetBaseTime = sdf.parse(assetBaseDate);
            assetProd.setAssetBaseDate(assetBaseTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        assetProd.setProdId(prodId);
        assetProd.setAssetValue(assetValue);
        assetProd.setCountGrowthRate(countGrowthRate);

        ProdService ps = new ProdServiceImpl();
        Integer addAssetNum = ps.insertAsset(assetProd);

        ReturnEntity re = new ReturnEntity();
        if (addAssetNum > 0) {
            re.setRetCode(ReturnCode.ADD_OK.getCode());
            re.setRetMsg(assetBaseTime + "基金净值" + ReturnCode.ADD_OK.getMsg());

        } else {
            re.setRetCode(ReturnCode.ADD_FAILED.getCode());
            re.setRetMsg(assetBaseTime + "基金净值" + ReturnCode.ADD_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void editProd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ProdInfo editProd = new ProdInfo();
        String secondTypeIdStr = req.getParameter("secondTypeId");
        Integer secondTypeId = 0;
        if (secondTypeIdStr!=null && !"".equals(secondTypeIdStr)){
            secondTypeId = Integer.parseInt(secondTypeIdStr);
        }
        editProd.setSecondTypeId(secondTypeId);

        String editProdName = req.getParameter("prodName");
        editProd.setProdName(editProdName);

        String editRemitCompany = req.getParameter("remitCompany");
        editProd.setRemitCompany(editRemitCompany);

        String editRateYear = req.getParameter("rateYear");
        editProd.setRateYear(editRateYear);

        String editMoneyType = req.getParameter("moneyType");
        editProd.setMoneyType(editMoneyType);

        String editOpeningTime = req.getParameter("openingTime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date openingTime = null;
        try {
            if (editOpeningTime!=null && !"".equals(editOpeningTime)){
                openingTime = sdf.parse(editOpeningTime);
                editProd.setOpeningTime(openingTime);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        editProd.setOpeningTime(openingTime);

        String editSubscribeCycleStr = req.getParameter("subscribeCycle");
        Integer editSubscribeCycle = 0;
        if (editSubscribeCycleStr!=null && !"".equals(editSubscribeCycleStr)){
            editSubscribeCycle = Integer.parseInt(editSubscribeCycleStr);
        }
        editProd.setSubscribeCycle(editSubscribeCycle);

        String editFundManageRate = req.getParameter("fundManageRate");
        editProd.setFundManageRate(editFundManageRate);

        String editSubscribeRate = req.getParameter("subscribeRate");
        editProd.setSubscribeRate(editSubscribeRate);

        String editMoneySubmitStyle = req.getParameter("moneySubmitStyle");
        editProd.setMoneySubmitStyle(editMoneySubmitStyle);

        String editRedeemCycleStr = req.getParameter("redeemCycle");
        Integer editRedeemCycle = 0;
        if (editRedeemCycleStr!=null && !"".equals(editRedeemCycleStr)){
            editRedeemCycle= Integer.parseInt(editRedeemCycleStr);
        }
        editProd.setRedeemCycle(editRedeemCycle);

        String editRedeemMinMoneyStr = req.getParameter("redeemMinMoney");
        BigDecimal editRedeemMinMoney = null;
        if (editRedeemMinMoneyStr!=null && !"".equals(editRedeemMinMoneyStr)){
            editRedeemMinMoney= BigDecimal.valueOf(Double.parseDouble(editRedeemMinMoneyStr));
        }
        editProd.setRedeemMinMoney(editRedeemMinMoney);

        String editRedeemFeeStr = req.getParameter("redeemFee");
        BigDecimal editRedeemFee = null;
        if (editRedeemFeeStr!=null && !"".equals(editRedeemFeeStr)){
            editRedeemFee= BigDecimal.valueOf(Double.parseDouble(editRedeemFeeStr));
        }
        editProd.setRedeemMinMoney(editRedeemFee);

        String editClosedCycleStr = req.getParameter("closedCycle");
        Integer editClosedCycle = 0;
        if (editClosedCycleStr!=null && !"".equals(editClosedCycleStr)){
            editClosedCycle= Integer.parseInt(editClosedCycleStr);
        }
        editProd.setClosedCycle(editClosedCycle);

        String editMinMoneyStr = req.getParameter("minMoney");
        BigDecimal editMinMoney = null;
        if (editMinMoneyStr!=null && !"".equals(editMinMoneyStr)){
            editMinMoney= BigDecimal.valueOf(Double.parseDouble(editMinMoneyStr));
        }
        editProd.setMinMoney(editMinMoney);

        String editProdIdStr = req.getParameter("prodId");
        Integer editProdId = 0;
        if (editProdIdStr!=null && !"".equals(editProdIdStr)){
            editProdId= Integer.parseInt(editProdIdStr);
        }
        editProd.setProdId(editProdId);

        ProdService ps = new ProdServiceImpl();
        Integer editNum = ps.editProd(editProd);
        ReturnEntity re = new ReturnEntity();
        //System.out.println("editNum"+editNum);
        if (editNum > 0) {
            re.setRetCode(ReturnCode.EDIT_OK.getCode());
            re.setRetMsg(ReturnCode.EDIT_OK.getMsg());

        } else {
            re.setRetCode(ReturnCode.EDIT_FAILED.getCode());
            re.setRetMsg(ReturnCode.EDIT_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void queryForEditProd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String queryProdIdStr = req.getParameter("queryProdId");
        Integer queryProdId = 0;
        if (queryProdIdStr!=null && !"".equals(queryProdIdStr)){
            queryProdId = Integer.parseInt(queryProdIdStr);
        }

        ProdService ps = new ProdServiceImpl();
        ProdInfo prodById = ps.getProdById(queryProdId);

        ReturnEntity re = new ReturnEntity();
        if (prodById!=null) {
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(prodById);
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void queryReviewerName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String reviewerIdStr = req.getParameter("reviewerId");

        Integer reviewerId = 0;
        if (reviewerIdStr!=null && !"".equals(reviewerIdStr)){
            reviewerId = Integer.parseInt(reviewerIdStr);
        }

        ProdService ps = new ProdServiceImpl();
        String reviewerName = ps.getReviewerName(reviewerId);
        ReturnEntity re = new ReturnEntity();
        if (reviewerName!=null){
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(reviewerName);
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }

    protected void NoReviewId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ReturnEntity re = new ReturnEntity();
        re.setRetCode(ReturnCode.NO_REVIEWER.getCode());
        re.setRetMsg(ReturnCode.NO_REVIEWER.getMsg());
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();


    }
}
